package com.aaa.project.controller;


import com.aaa.entity.EEmpInfo;
import com.aaa.entity.EMessage;
import com.aaa.project.service.IEEmpInfoService;
import com.aaa.project.service.IEMessageService;
import com.aaa.util.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * <p>
 * 系统通知表 前端控制器
 * </p>
 *
 * @author yuyongli
 * @since 2023-02-06
 */
@RestController
@RequestMapping("/e-message")
public class EMessageController {

    @Autowired
    private IEMessageService eMessageService;

    @Autowired
    private IEEmpInfoService empInfoService;


    @PostMapping("sendMessage")
    public Result sendMessage(EMessage eMessage, Principal principal){
        String telephone = principal.getName();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("telephone",telephone);
        EEmpInfo one = empInfoService.getOne(queryWrapper);
        eMessage.setEid(one.getId());
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String format = dateTimeFormatter.format(now);
        eMessage.setGmtDate(format);
        eMessage.setIsDeleted(0);
        return new Result(eMessageService.saveOrUpdate(eMessage));
    }

    @PostMapping("deleteMessageById/{id}")
    public Result deleteMessageById(@PathVariable String id){
        return new Result(eMessageService.removeById(id));
    }
}

